小寫轉大寫金額[SQL SERVER]

穩萊

小寫轉大寫金額[SQL SERVER]
 
原作在SQL 2000中有一些問題。修正錯誤並在SQL 2000中運行通過。
/********************************************************
作者:(wleii165@yahoo.com)
版本:1.0
創建時間:20020227
修改時間:
功能:小寫金額轉換成大寫
參數:n_LowerMoney 小寫金額
          v_TransType 種類 -- 1: directly translate, 0: read it in words
輸出:大寫金額
********************************************************/
Create PROCEDURE dbo.L2U
(
@n_LowerMoney numeric(15,2),
@v_TransType int,
@RET VARCHAR(200) output
)
 AS
 
Declare @v_LowerStr VARCHAR(200) -- 小寫金額
Declare @v_UpperPart VARCHAR(200)
Declare @v_UpperStr VARCHAR(200) -- 大寫金額
Declare @i_I int

set nocount on

select @v_LowerStr = LTRIM(RTRIM(STR(@n_LowerMoney,20,2))) --四捨五入為指定的精度並刪除數據左右空格

select @i_I = 1
select @v_UpperStr = '

while ( @i_I <= len(@v_LowerStr))
begin
      select @v_UpperPart = case substring(@v_LowerStr,len(@v_LowerStr) - @i_I + 1,1)
                            WHEN  '.' THEN  '元'
                            WHEN  '0' THEN  '零'
                            WHEN  '1' THEN  '壹'
                            WHEN  '2' THEN  '貳'
                            WHEN  '3' THEN  '三'
                            WHEN  '4' THEN  '肆'
                            WHEN  '5' THEN  '伍'
                            WHEN  '6' THEN  '陸'
                            WHEN  '7' THEN  '柒'
                            WHEN  '8' THEN  '捌'
                            WHEN  '9' THEN  '玖'
                            END
                          +
                            case @i_I
                            WHEN  1  THEN  '分'
                            WHEN  2  THEN  '角'
                            WHEN  3  THEN  '
                            WHEN  4  THEN  '
                            WHEN  5  THEN  '拾'
                            WHEN  6  THEN  '佰'
                            WHEN  7  THEN  '仟'
                            WHEN  8  THEN  '萬'
                            WHEN  9  THEN  '拾'
                            WHEN  10  THEN  '佰'
                            WHEN  11  THEN  '仟'
                            WHEN  12  THEN  '億'
                            WHEN  13  THEN  '拾'
                            WHEN  14  THEN  '佰'
                            WHEN  15  THEN  '仟'
                            WHEN  16  THEN  '萬'
                            ELSE '
                            END
select @v_UpperStr = @v_UpperPart + @v_UpperStr
select @i_I = @i_I + 1
end

--------print  '//v_UpperStr ='+@v_UpperStr +'//'

if ( @v_TransType=0 )
begin
select @v_UpperStr = REPLACE(@v_UpperStr,'零拾','零')
select @v_UpperStr = REPLACE(@v_UpperStr,'零佰','零')
select @v_UpperStr = REPLACE(@v_UpperStr,'零仟','零')
select @v_UpperStr = REPLACE(@v_UpperStr,'零零零','零')
select @v_UpperStr = REPLACE(@v_UpperStr,'零零','零')
select @v_UpperStr = REPLACE(@v_UpperStr,'零角零分','整')
select @v_UpperStr = REPLACE(@v_UpperStr,'零分','整')
select @v_UpperStr = REPLACE(@v_UpperStr,'零角','零')
select @v_UpperStr = REPLACE(@v_UpperStr,'零億零萬零元','億元')
select @v_UpperStr = REPLACE(@v_UpperStr,'億零萬零元','億元')
select @v_UpperStr = REPLACE(@v_UpperStr,'零億零萬','億')
select @v_UpperStr = REPLACE(@v_UpperStr,'零萬零元','萬元')
select @v_UpperStr = REPLACE(@v_UpperStr,'萬零元','萬元')
select @v_UpperStr = REPLACE(@v_UpperStr,'零億','億')
select @v_UpperStr = REPLACE(@v_UpperStr,'零萬','萬')
select @v_UpperStr = REPLACE(@v_UpperStr,'零元','元')
select @v_UpperStr = REPLACE(@v_UpperStr,'零零','零')
end

-- 對壹元以下的金額的處理
if ( substring(@v_UpperStr,1,1)='元' )
begin
     select @v_UpperStr = substring(@v_UpperStr,2,(len(@v_UpperStr) - 1))
end

if (substring(@v_UpperStr,1,1)= '零')
begin
     select @v_UpperStr = substring(@v_UpperStr,2,(len(@v_UpperStr) - 1))
end

if (substring(@v_UpperStr,1,1)='角')
begin
     select @v_UpperStr = substring(@v_UpperStr,2,(len(@v_UpperStr) - 1))
end

if ( substring(@v_UpperStr,1,1)='分')
begin
     select @v_UpperStr = substring(@v_UpperStr,2,(len(@v_UpperStr) - 1))
end

if (substring(@v_UpperStr,1,1)='整')
begin
     select @v_UpperStr = '零元整'
end

select @ret=@v_UpperStr

GO

調用過程:

declare @ret varchar(200)

exec L2U 567983.897,1,@ret output

select @ret
 

 

 給當前日誌評分:
Loading Vote
正在讀取評分資料...


文章來自: Tank部落格
引用通告: 查看所有引用 | 我要引用此文章
Tags:
相關日誌:

評論: 0 | 引用: 0 | 查看次數: -
發表評論
暱 稱:
密 碼: 遊客發言不需要密碼.
內 容:
驗證碼: 驗證碼
選 項:
雖然發表評論不用註冊,但是為了保護您的發言權,建議您註冊帳號.